package com.mock.ems.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.mock.base.util.CollectionUtil;
import com.mock.base.util.DateUtil;
import com.mock.base.util.StringUtil;
import com.mock.ems.config.UserManager;

import java.util.*;

public class TrainOrderController extends Controller {

    public void index(){
        renderJsp("/page/train/train_list.jsp");
    }

    public void pagination() {

        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");

        String selectSQL = "select * ";
        StringBuilder exceptSelectSQL = new StringBuilder(" from train_purchase_record where 1 = 1");
        List<Object> params = new ArrayList<Object>();

        String nickName = getPara("nickName");
        if (StringUtil.isNotEmpty(nickName)) {
            List<Record> userRecordList = Db.find("select id from user where nick_name like ? ",'%' + nickName.trim() + '%');
            if (CollectionUtil.isNotEmpty(userRecordList)) {
                exceptSelectSQL.append(" and user_id in ( ");
                userRecordList.stream().forEach(item -> {
                    exceptSelectSQL.append("?,");
                    params.add(item.getLong("id"));
                });
                exceptSelectSQL.deleteCharAt(exceptSelectSQL.length() - 1);
                exceptSelectSQL.append(") ");
            }
        }

        Date begin_time = DateUtil.parseToDate(getPara("begin_time"),DateUtil.FULL_PATTERN);
        Date end_time = DateUtil.parseToDate(getPara("end_time"),DateUtil.FULL_PATTERN);

        if (null != begin_time && end_time == null) {
            exceptSelectSQL.append(" and create_time >= ? ");
            params.add(begin_time);
        }

        if (null == begin_time && end_time != null) {
            exceptSelectSQL.append(" and create_time <= ? ");
            params.add(end_time);
        }

        if (null != begin_time && null != end_time) {
            exceptSelectSQL.append(" and create_time >= ? and create_time <= ? ");
            params.add(begin_time);
            params.add(end_time);
        }
        exceptSelectSQL.append(" order by id desc ");

        Page<Record> recordList = null;
        if (params.size() == 0) {
            recordList = Db.paginate(pageNumber, pageSize, selectSQL, exceptSelectSQL.toString());
        }else {
            recordList = Db.paginate(pageNumber, pageSize, "select * ", exceptSelectSQL.toString(),params.toArray());
        }

        if (CollectionUtil.isNotEmpty(recordList.getList())) {
            recordList.getList().forEach(item -> {
                Long userId = item.getLong("user_id");
                Record user = UserManager.getUser(userId);
                if(user == null){
                    user = Db.findById("user",userId);
                    if(user != null){
                        UserManager.setUser(user);
                    }
                }
                item.set("nick_name",user == null ? "未知" : user.get("nick_name"));
                item.set("phone",user == null ? "未知" :user.get("phone"));

                Record train = Db.findById("train",item.getLong("train_id"));
                if (null != train) {
                    item.set("title",train.get("title"));
                }
            });
        }

        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put("total", recordList.getTotalRow());
        returnMap.put("rows", recordList.getList());
        renderJson(returnMap);
    }

}
